<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"always","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="声明，本教程摘录于菜鸟官网，非本人原创。文中部分内容来自于个人工作积累编写，仅供学习参考。  原创网址： 菜鸟教程 介绍NPM 是随同 NodeJS 一起安装的包管理工具，能解决 NodeJS 代码部署上的很多问题，常见的使用场景有以下几种：  允许用户从 NPM 服务器下载别人编写的第三方包到本地使用。 允许用户从 NPM 服务器下载并安装别人编写的命令行程序到本地使用。 允许用户将自己编写的">
<meta property="og:type" content="article">
<meta property="og:title" content="npm 使用教程">
<meta property="og:url" content="http://yoursite.com/2019/04/29/npm-%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="Albert Guo">
<meta property="og:description" content="声明，本教程摘录于菜鸟官网，非本人原创。文中部分内容来自于个人工作积累编写，仅供学习参考。  原创网址： 菜鸟教程 介绍NPM 是随同 NodeJS 一起安装的包管理工具，能解决 NodeJS 代码部署上的很多问题，常见的使用场景有以下几种：  允许用户从 NPM 服务器下载别人编写的第三方包到本地使用。 允许用户从 NPM 服务器下载并安装别人编写的命令行程序到本地使用。 允许用户将自己编写的">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-04-29T11:28:56.000Z">
<meta property="article:modified_time" content="2020-06-29T12:03:35.143Z">
<meta property="article:author" content="Albert Guo">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2019/04/29/npm-%E4%BD%BF%E7%94%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>npm 使用教程 | Albert Guo</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Albert Guo</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-bars fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-address-card fa-fw"></i>简介</a>

  </li>
        <li class="menu-item menu-item-webpack">

    <a href="/webpack/" rel="section"><i class="fa fa-bookmark fa-fw"></i>webpack</a>

  </li>
        <li class="menu-item menu-item-javascript">

    <a href="/JavaScript/" rel="section"><i class="fa fa-bookmark fa-fw"></i>ECMAScript 5.1</a>

  </li>
        <li class="menu-item menu-item-ecmascript6">

    <a href="/ECMAScript6/" rel="section"><i class="fa fa-bookmark fa-fw"></i>ECMAScript 6</a>

  </li>
        <li class="menu-item menu-item-react">

    <a href="/React/" rel="section"><i class="fa fa-bookmark fa-fw"></i>React</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/04/29/npm-%E4%BD%BF%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://inews.gtimg.com/newsapp_ls/0/12122532695/0">
      <meta itemprop="name" content="Albert Guo">
      <meta itemprop="description" content="郭鹏松的个人博客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Albert Guo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          npm 使用教程
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-04-29 19:28:56" itemprop="dateCreated datePublished" datetime="2019-04-29T19:28:56+08:00">2019-04-29</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-06-29 20:03:35" itemprop="dateModified" datetime="2020-06-29T20:03:35+08:00">2020-06-29</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index"><span itemprop="name">nodejs</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>声明，本教程摘录于菜鸟官网，非本人原创。文中部分内容来自于个人工作积累编写，仅供学习参考。</p>
</blockquote>
<p>原创网址： <a href="https://www.runoob.com/nodejs/nodejs-npm.html" target="_blank" rel="noopener">菜鸟教程</a></p>
<h1 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h1><p>NPM 是随同 NodeJS 一起安装的包管理工具，能解决 NodeJS 代码部署上的很多问题，常见的使用场景有以下几种：</p>
<ul>
<li>允许用户从 NPM 服务器下载别人编写的第三方包到本地使用。</li>
<li>允许用户从 NPM 服务器下载并安装别人编写的命令行程序到本地使用。</li>
<li>允许用户将自己编写的包或命令行程序上传到 NPM 服务器供别人使用。</li>
</ul>
<p>由于新版的 nodejs 已经集成了 npm ，所以之前 npm 也一并安装好了。同样可以通过输入 <code>&quot;npm -v&quot;</code> 来测试是否成功安装。命令如下，出现版本提示表示安装成功:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ npm -v</span><br><span class="line">2.3.0</span><br></pre></td></tr></table></figure>

<p>如果你安装的是旧版本的 npm，可以很容易得通过 npm 命令来升级，命令如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ sudo npm install npm -g</span><br><span class="line">&#x2F;usr&#x2F;local&#x2F;bin&#x2F;npm -&gt; &#x2F;usr&#x2F;local&#x2F;lib&#x2F;node_modules&#x2F;npm&#x2F;bin&#x2F;npm-cli.js</span><br><span class="line">npm@2.14.2 &#x2F;usr&#x2F;local&#x2F;lib&#x2F;node_modules&#x2F;npm</span><br></pre></td></tr></table></figure>

<p>如果是 Window 系统使用以下命令即可：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install npm -g</span><br></pre></td></tr></table></figure>

<p>使用淘宝镜像的命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g cnpm --registry&#x3D;https:&#x2F;&#x2F;registry.npm.taobao.org</span><br></pre></td></tr></table></figure>

<h1 id="使用-npm-命令安装模块"><a href="#使用-npm-命令安装模块" class="headerlink" title="使用 npm 命令安装模块"></a>使用 npm 命令安装模块</h1><p><code>npm</code> 安装 Node.js 模块语法格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm install &lt;Module Name&gt;</span><br></pre></td></tr></table></figure>

<p>以下实例，我们使用 npm 命令安装常用的 Node.js web 框架模块 express :</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm install express</span><br></pre></td></tr></table></figure>

<p>安装好之后，<code>express</code> 包就放在了工程目录下的 <code>node_modules</code> 目录中，因此在代码中只需要通过 <code>require(&#39;express&#39;)</code> 的方式就好，无需指定第三方包路径。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> express = <span class="built_in">require</span>(<span class="string">'express'</span>);</span><br></pre></td></tr></table></figure>

<h1 id="全局安装与本地安装"><a href="#全局安装与本地安装" class="headerlink" title="全局安装与本地安装"></a>全局安装与本地安装</h1><p>==npm 的包安装分为本地安装（local）、全局安装（global）两种。== 至于选择哪种方式来安装，取决于我们如何使用这个包。</p>
<ul>
<li>如果你自己的模块依赖于某个包，并通过 Node.js 的 require 加载，那么你应该选择本地安装，这种方式也是 npm install 命令的默认行为。</li>
<li>如果你想将包作为一个命令行工具，（比如 grunt CLI），那么你应该选择全局安装。</li>
</ul>
<p>从敲的命令行来看，差别只是有没有 <code>-g</code> 而已，比如</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm install express      # 本地安装</span><br><span class="line">npm install express -g   # 全局安装</span><br></pre></td></tr></table></figure>

<p>如果出现以下错误：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm err! Error: connect ECONNREFUSED 127.0.0.1:8087</span><br></pre></td></tr></table></figure>

<p>解决办法为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm config set proxy null</span><br></pre></td></tr></table></figure>

<h2 id="本地安装"><a href="#本地安装" class="headerlink" title="本地安装"></a>本地安装</h2><ol>
<li>将安装包放在 <code>./node_modules</code> 下（==运行 npm 命令时所在的目录==），如果没有 <code>node_modules</code> 目录，会在当前执行 <code>npm</code> 命令的目录下生成 <code>node_modules</code> 目录。</li>
<li>可以通过 <code>require()</code> 来引入本地安装的包。</li>
</ol>
<h2 id="全局安装"><a href="#全局安装" class="headerlink" title="全局安装"></a>全局安装</h2><ol>
<li>将安装包放在 <code>/usr/local</code> 下或者你 <code>node</code> 的安装目录。</li>
<li>可以直接在命令行里使用。<br>如果你希望具备两者功能，则需要在两个地方安装它或使用 <code>npm link</code>。</li>
</ol>
<p>接下来我们使用全局方式安装 express 。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm install express -g</span><br></pre></td></tr></table></figure>

<p>安装过程输出如下内容，第一行输出了模块的版本号及安装位置。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">express@4.13.3 node_modules&#x2F;express</span><br><span class="line">├── escape-html@1.0.2</span><br><span class="line">├── range-parser@1.0.2</span><br><span class="line">├── merge-descriptors@1.0.0</span><br><span class="line">├── array-flatten@1.1.1</span><br><span class="line">├── cookie@0.1.3</span><br><span class="line">├── utils-merge@1.0.0</span><br><span class="line">├── parseurl@1.3.0</span><br><span class="line">├── cookie-signature@1.0.6</span><br><span class="line">├── methods@1.1.1</span><br><span class="line">├── fresh@0.3.0</span><br><span class="line">├── vary@1.0.1</span><br><span class="line">├── path-to-regexp@0.1.7</span><br><span class="line">├── content-type@1.0.1</span><br><span class="line">├── etag@1.7.0</span><br><span class="line">├── serve-static@1.10.0</span><br><span class="line">├── content-disposition@0.5.0</span><br><span class="line">├── depd@1.0.1</span><br><span class="line">├── qs@4.0.0</span><br><span class="line">├── finalhandler@0.4.0 (unpipe@1.0.0)</span><br><span class="line">├── on-finished@2.3.0 (ee-first@1.1.1)</span><br><span class="line">├── proxy-addr@1.0.8 (forwarded@0.1.0, ipaddr.js@1.0.1)</span><br><span class="line">├── debug@2.2.0 (ms@0.7.1)</span><br><span class="line">├── type-is@1.6.8 (media-typer@0.3.0, mime-types@2.1.6)</span><br><span class="line">├── accepts@1.2.12 (negotiator@0.5.3, mime-types@2.1.6)</span><br><span class="line">└── send@0.13.0 (destroy@1.0.3, statuses@1.2.1, ms@0.7.1, mime@1.3.4, http-errors@1.3.1)</span><br></pre></td></tr></table></figure>

<h2 id="查看安装信息"><a href="#查看安装信息" class="headerlink" title="查看安装信息"></a>查看安装信息</h2><p>你可以使用以下命令来查看所有全局安装的模块：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$ npm list -g</span><br><span class="line"></span><br><span class="line">├─┬ cnpm@4.3.2</span><br><span class="line">│ ├── auto-correct@1.0.0</span><br><span class="line">│ ├── bagpipe@0.3.5</span><br><span class="line">│ ├── colors@1.1.2</span><br><span class="line">│ ├─┬ commander@2.9.0</span><br><span class="line">│ │ └── graceful-readlink@1.0.1</span><br><span class="line">│ ├─┬ cross-spawn@0.2.9</span><br><span class="line">│ │ └── lru-cache@2.7.3</span><br><span class="line">……</span><br></pre></td></tr></table></figure>

<p>如果要查看某个模块的版本号，可以使用命令如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ npm list grunt</span><br><span class="line"></span><br><span class="line">projectName@projectVersion &#x2F;path&#x2F;to&#x2F;project&#x2F;folder</span><br><span class="line">└── grunt@0.4.1</span><br></pre></td></tr></table></figure>

<h1 id="几种安装方式的区别-g、–save、–save-dev"><a href="#几种安装方式的区别-g、–save、–save-dev" class="headerlink" title="几种安装方式的区别(-g、–save、–save-dev)"></a>几种安装方式的区别(-g、–save、–save-dev)</h1><p>使用 <code>npm install</code> 安装模块的模块的时候 ，一般会使用下面这几种命令形式:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">npm install moduleName # 安装模块到项目目录下</span><br><span class="line"> </span><br><span class="line">npm install -g moduleName # -g 的意思是将模块安装到全局，具体安装到磁盘哪个位置，要看 npm config prefix 的位置。</span><br><span class="line"> </span><br><span class="line">npm install -save moduleName # -save 的意思是将模块安装到项目目录下，并在 package 文件的 dependencies 节点写入依赖。</span><br><span class="line"> </span><br><span class="line">npm install -save-dev moduleName # -save-dev 的意思是将模块安装到项目目录下，并在 package 文件的 devDependencies 节点写入依赖。</span><br></pre></td></tr></table></figure>

<h2 id="npm-install-moduleName-命令"><a href="#npm-install-moduleName-命令" class="headerlink" title="npm install moduleName 命令"></a>npm install moduleName 命令</h2><ol>
<li>安装模块到项目 <code>node_modules</code> 目录下。</li>
<li>不会将模块依赖写入 <code>devDependencies</code> 或 <code>dependencies</code> 节点。</li>
<li>运行 <code>npm install</code> 初始化项目时==不会下载模块。==</li>
</ol>
<h2 id="npm-install-g-moduleName-命令"><a href="#npm-install-g-moduleName-命令" class="headerlink" title="npm install -g moduleName 命令"></a>npm install -g moduleName 命令</h2><ol>
<li>安装模块到全局，不会在项目 <code>node_modules</code> 目录中保存模块包。</li>
<li>不会将模块依赖写入 <code>devDependencies</code> 或 <code>dependencies</code> 节点。</li>
<li>运行 npm install 初始化项目时==不会下载模块。==</li>
</ol>
<h2 id="npm-install-save-moduleName-命令"><a href="#npm-install-save-moduleName-命令" class="headerlink" title="npm install -save moduleName 命令"></a>npm install -save moduleName 命令</h2><ol>
<li>安装模块到项目 <code>node_modules</code> 目录下。</li>
<li>会将模块依赖写入 <code>dependencies</code> 节点。</li>
<li>运行 <code>npm install</code> 初始化项目时，会将模块下载到项目目录下。</li>
<li>运行 <code>npm install --production</code>  或者注明 <code>NODE_ENV</code> 变量值为 <code>production</code> 时，会自动下载模块到 <code>node_modules</code> 目录中。</li>
</ol>
<h2 id="npm-install-save-dev-moduleName-命令"><a href="#npm-install-save-dev-moduleName-命令" class="headerlink" title="npm install -save-dev moduleName 命令"></a>npm install -save-dev moduleName 命令</h2><ol>
<li>安装模块到项目 <code>node_modules</code> 目录下。</li>
<li>会将模块依赖写入 <code>devDependencies</code> 节点。</li>
<li>运行 <code>npm install</code> 初始化项目时，会将模块下载到项目目录下。</li>
<li>运行 <code>npm install --production</code>  或者注明 <code>NODE_ENV</code> 变量值为 <code>production</code> 时，不会自动下载模块到 <code>node_modules</code> 目录中。</li>
</ol>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p><code>devDependencies</code> 节点下的模块是我们在开发时需要用的，比如项目中使用的 <code>gulp</code> ，压缩 <code>css、js</code> 的模块。这些模块在我们的项目部署后是不需要的，所以我们可以使用 <code>-save-dev</code> 的形式安装。像 <code>express</code> 这些模块是项目运行必备的，应该安装在 <code>dependencies</code> 节点下，所以我们应该使用 <code>-save</code> 的形式安装。</p>
<p>在安装一个要打包到生产环境的安装包时，你应该使用 <code>npm install --save</code>，如果你在安装一个用于开发环境的安装包（例如，linter, 测试库等），你应该使用 <code>npm install --save-dev</code>。</p>
<h1 id="使用-package-json"><a href="#使用-package-json" class="headerlink" title="使用 package.json"></a>使用 package.json</h1><p><code>package.json</code> 位于模块的目录下，用于定义包的属性。接下来让我们来看下 <code>express</code> 包的 <code>package.json</code> 文件，位于 <code>node_modules/express/package.json</code> 内容：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">"name"</span>: <span class="string">"express"</span>, <span class="comment">// 项目名称</span></span><br><span class="line">  <span class="string">"description"</span>: <span class="string">"Fast, unopinionated, minimalist web framework"</span>, <span class="comment">// 项目描述</span></span><br><span class="line">  <span class="string">"version"</span>: <span class="string">"4.13.3"</span>, <span class="comment">// 版本</span></span><br><span class="line">  <span class="string">"private"</span>: <span class="literal">true</span>, <span class="comment">// 是否是私有项目（true 是）</span></span><br><span class="line">  <span class="string">"author"</span>: &#123;</span><br><span class="line">    <span class="string">"name"</span>: <span class="string">"TJ Holowaychuk"</span>, <span class="comment">// 作者姓名</span></span><br><span class="line">    <span class="string">"email"</span>: <span class="string">"tj@vision-media.ca"</span> <span class="comment">// 作者邮箱</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"contributors"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">"name"</span>: <span class="string">"Aaron Heckmann"</span>,</span><br><span class="line">      <span class="string">"email"</span>: <span class="string">"aaron.heckmann+github@gmail.com"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">"name"</span>: <span class="string">"Ciaran Jessup"</span>,</span><br><span class="line">      <span class="string">"email"</span>: <span class="string">"ciaranj@gmail.com"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ], <span class="comment">// 其他贡献者</span></span><br><span class="line">  <span class="string">"license"</span>: <span class="string">"MIT"</span>, <span class="comment">// 开源协议</span></span><br><span class="line">  <span class="string">"repository"</span>: &#123;</span><br><span class="line">    <span class="string">"type"</span>: <span class="string">"git"</span>,</span><br><span class="line">    <span class="string">"url"</span>: <span class="string">"git+https://github.com/strongloop/express.git"</span></span><br><span class="line">  &#125;, <span class="comment">// 仓库地址</span></span><br><span class="line">  <span class="string">"homepage"</span>: <span class="string">"http://expressjs.com/"</span>, <span class="comment">// 主页网址</span></span><br><span class="line">  <span class="string">"keywords"</span>: [</span><br><span class="line">    <span class="string">"express"</span>,</span><br><span class="line">    <span class="string">"framework"</span>,</span><br><span class="line">    <span class="string">"sinatra"</span></span><br><span class="line">  ], <span class="comment">// 关键字</span></span><br><span class="line">  <span class="string">"dependencies"</span>: &#123;</span><br><span class="line">    <span class="string">"accepts"</span>: <span class="string">"~1.2.12"</span>,</span><br><span class="line">    <span class="string">"array-flatten"</span>: <span class="string">"1.1.1"</span>,</span><br><span class="line">    <span class="string">"content-disposition"</span>: <span class="string">"0.5.0"</span>,</span><br><span class="line">    <span class="string">"content-type"</span>: <span class="string">"~1.0.1"</span>,</span><br><span class="line">    <span class="string">"cookie"</span>: <span class="string">"0.1.3"</span>,</span><br><span class="line">    <span class="string">"cookie-signature"</span>: <span class="string">"1.0.6"</span>,</span><br><span class="line">    <span class="string">"debug"</span>: <span class="string">"~2.2.0"</span>,</span><br><span class="line">    <span class="string">"depd"</span>: <span class="string">"~1.0.1"</span>,</span><br><span class="line">    <span class="string">"escape-html"</span>: <span class="string">"1.0.2"</span>,</span><br><span class="line">    <span class="string">"etag"</span>: <span class="string">"~1.7.0"</span>,</span><br><span class="line">    <span class="string">"finalhandler"</span>: <span class="string">"0.4.0"</span>,</span><br><span class="line">    <span class="string">"fresh"</span>: <span class="string">"0.3.0"</span>,</span><br><span class="line">    <span class="string">"merge-descriptors"</span>: <span class="string">"1.0.0"</span>,</span><br><span class="line">    <span class="string">"methods"</span>: <span class="string">"~1.1.1"</span>,</span><br><span class="line">    <span class="string">"on-finished"</span>: <span class="string">"~2.3.0"</span>,</span><br><span class="line">    <span class="string">"parseurl"</span>: <span class="string">"~1.3.0"</span>,</span><br><span class="line">    <span class="string">"path-to-regexp"</span>: <span class="string">"0.1.7"</span>,</span><br><span class="line">    <span class="string">"proxy-addr"</span>: <span class="string">"~1.0.8"</span>,</span><br><span class="line">    <span class="string">"qs"</span>: <span class="string">"4.0.0"</span>,</span><br><span class="line">    <span class="string">"range-parser"</span>: <span class="string">"~1.0.2"</span>,</span><br><span class="line">    <span class="string">"send"</span>: <span class="string">"0.13.0"</span>,</span><br><span class="line">    <span class="string">"serve-static"</span>: <span class="string">"~1.10.0"</span>,</span><br><span class="line">    <span class="string">"type-is"</span>: <span class="string">"~1.6.6"</span>,</span><br><span class="line">    <span class="string">"utils-merge"</span>: <span class="string">"1.0.0"</span>,</span><br><span class="line">    <span class="string">"vary"</span>: <span class="string">"~1.0.1"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"devDependencies"</span>: &#123;</span><br><span class="line">    <span class="string">"after"</span>: <span class="string">"0.8.1"</span>,</span><br><span class="line">    <span class="string">"ejs"</span>: <span class="string">"2.3.3"</span>,</span><br><span class="line">    <span class="string">"istanbul"</span>: <span class="string">"0.3.17"</span>,</span><br><span class="line">    <span class="string">"marked"</span>: <span class="string">"0.3.5"</span>,</span><br><span class="line">    <span class="string">"mocha"</span>: <span class="string">"2.2.5"</span>,</span><br><span class="line">    <span class="string">"should"</span>: <span class="string">"7.0.2"</span>,</span><br><span class="line">    <span class="string">"supertest"</span>: <span class="string">"1.0.1"</span>,</span><br><span class="line">    <span class="string">"body-parser"</span>: <span class="string">"~1.13.3"</span>,</span><br><span class="line">    <span class="string">"connect-redis"</span>: <span class="string">"~2.4.1"</span>,</span><br><span class="line">    <span class="string">"cookie-parser"</span>: <span class="string">"~1.3.5"</span>,</span><br><span class="line">    <span class="string">"cookie-session"</span>: <span class="string">"~1.2.0"</span>,</span><br><span class="line">    <span class="string">"express-session"</span>: <span class="string">"~1.11.3"</span>,</span><br><span class="line">    <span class="string">"jade"</span>: <span class="string">"~1.11.0"</span>,</span><br><span class="line">    <span class="string">"method-override"</span>: <span class="string">"~2.3.5"</span>,</span><br><span class="line">    <span class="string">"morgan"</span>: <span class="string">"~1.6.1"</span>,</span><br><span class="line">    <span class="string">"multiparty"</span>: <span class="string">"~4.1.2"</span>,</span><br><span class="line">    <span class="string">"vhost"</span>: <span class="string">"~3.0.1"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"engines"</span>: &#123;</span><br><span class="line">    <span class="string">"node"</span>: <span class="string">"&gt;= 0.10.0"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"files"</span>: [</span><br><span class="line">    <span class="string">"LICENSE"</span>,</span><br><span class="line">    <span class="string">"History.md"</span>,</span><br><span class="line">    <span class="string">"Readme.md"</span>,</span><br><span class="line">    <span class="string">"index.js"</span>,</span><br><span class="line">    <span class="string">"lib/"</span></span><br><span class="line">  ],</span><br><span class="line">  <span class="string">"scripts"</span>: &#123;</span><br><span class="line">    <span class="string">"test"</span>: <span class="string">"mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/"</span>,</span><br><span class="line">    <span class="string">"test-ci"</span>: <span class="string">"istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/"</span>,</span><br><span class="line">    <span class="string">"test-cov"</span>: <span class="string">"istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/"</span>,</span><br><span class="line">    <span class="string">"test-tap"</span>: <span class="string">"mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"gitHead"</span>: <span class="string">"ef7ad681b245fba023843ce94f6bcb8e275bbb8e"</span>,</span><br><span class="line">  <span class="string">"bugs"</span>: &#123;</span><br><span class="line">    <span class="string">"url"</span>: <span class="string">"https://github.com/strongloop/express/issues"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"_id"</span>: <span class="string">"express@4.13.3"</span>,</span><br><span class="line">  <span class="string">"_shasum"</span>: <span class="string">"ddb2f1fb4502bf33598d2b032b037960ca6c80a3"</span>,</span><br><span class="line">  <span class="string">"_from"</span>: <span class="string">"express@*"</span>,</span><br><span class="line">  <span class="string">"_npmVersion"</span>: <span class="string">"1.4.28"</span>,</span><br><span class="line">  <span class="string">"_npmUser"</span>: &#123;</span><br><span class="line">    <span class="string">"name"</span>: <span class="string">"dougwilson"</span>,</span><br><span class="line">    <span class="string">"email"</span>: <span class="string">"doug@somethingdoug.com"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"maintainers"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">"name"</span>: <span class="string">"tjholowaychuk"</span>,</span><br><span class="line">      <span class="string">"email"</span>: <span class="string">"tj@vision-media.ca"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">"name"</span>: <span class="string">"jongleberry"</span>,</span><br><span class="line">      <span class="string">"email"</span>: <span class="string">"jonathanrichardong@gmail.com"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="string">"dist"</span>: &#123;</span><br><span class="line">    <span class="string">"shasum"</span>: <span class="string">"ddb2f1fb4502bf33598d2b032b037960ca6c80a3"</span>,</span><br><span class="line">    <span class="string">"tarball"</span>: <span class="string">"http://registry.npmjs.org/express/-/express-4.13.3.tgz"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"directories"</span>: &#123;&#125;,</span><br><span class="line">  <span class="string">"_resolved"</span>: <span class="string">"https://registry.npmjs.org/express/-/express-4.13.3.tgz"</span>,</span><br><span class="line">  <span class="string">"readme"</span>: <span class="string">"ERROR: No README data found!"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Package-json-属性说明"><a href="#Package-json-属性说明" class="headerlink" title="Package.json 属性说明"></a>Package.json 属性说明</h1><ul>
<li>name - 包名。</li>
<li>version - 包的版本号。</li>
<li>description - 包的描述。</li>
<li>homepage - 包的官网 url 。</li>
<li>author - 包的作者姓名。</li>
<li>contributors - 包的其他贡献者姓名。</li>
<li>dependencies - 依赖包列表。如果依赖包没有安装，npm 会自动将依赖包安装在 node_module 目录下。</li>
<li>repository - 包代码存放的地方的类型，可以是 git 或 svn，git 可在 Github 上。</li>
<li>main - main 字段指定了程序的主入口文件，require(‘moduleName’) 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。</li>
<li>keywords - 关键字</li>
</ul>
<h1 id="卸载模块"><a href="#卸载模块" class="headerlink" title="卸载模块"></a>卸载模块</h1><p>我们可以使用以下命令来卸载 Node.js 模块。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm uninstall express</span><br></pre></td></tr></table></figure>

<p>卸载后，你可以到 /node_modules/ 目录下查看包是否还存在，或者使用以下命令查看：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm ls</span><br></pre></td></tr></table></figure>

<h1 id="更新模块"><a href="#更新模块" class="headerlink" title="更新模块"></a>更新模块</h1><p>我们可以使用以下命令更新指定模块，例如更新 <code>express</code> 模块：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm update express</span><br></pre></td></tr></table></figure>

<h1 id="搜索模块"><a href="#搜索模块" class="headerlink" title="搜索模块"></a>搜索模块</h1><p>使用以下来搜索模块，例如搜索 <code>express</code> 模块：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm search express</span><br></pre></td></tr></table></figure>

<h1 id="创建模块"><a href="#创建模块" class="headerlink" title="创建模块"></a>创建模块</h1><p>创建模块，package.json 文件是必不可少的。我们可以使用 NPM 生成 package.json 文件，生成的文件包含了基本的结果。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">$ npm init</span><br><span class="line"></span><br><span class="line">This utility will walk you through creating a package.json file.</span><br><span class="line">It only covers the most common items, and tries to guess sensible defaults.</span><br><span class="line"></span><br><span class="line">See &#96;npm help json&#96; for definitive documentation on these fields</span><br><span class="line">and exactly what they do.</span><br><span class="line"></span><br><span class="line">Use &#96;npm install &lt;pkg&gt; --save&#96; afterwards to install a package and</span><br><span class="line">save it as a dependency in the package.json file.</span><br><span class="line"></span><br><span class="line">Press ^C at any time to quit.</span><br><span class="line"></span><br><span class="line">name: (node_modules) runoob                   # 模块名</span><br><span class="line">version: (1.0.0) </span><br><span class="line">description: Node.js 测试模块(www.runoob.com)  # 描述</span><br><span class="line">entry point: (index.js) </span><br><span class="line">test command: make test</span><br><span class="line">git repository: https:&#x2F;&#x2F;github.com&#x2F;runoob&#x2F;runoob.git  # Github 地址</span><br><span class="line">keywords: </span><br><span class="line">author: </span><br><span class="line">license: (ISC) </span><br><span class="line">About to write to ……&#x2F;node_modules&#x2F;package.json:      # 生成地址</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  &quot;name&quot;: &quot;runoob&quot;,</span><br><span class="line">  &quot;version&quot;: &quot;1.0.0&quot;,</span><br><span class="line">  &quot;description&quot;: &quot;Node.js 测试模块(www.runoob.com)&quot;,</span><br><span class="line">  ……</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Is this ok? (yes) yes</span><br></pre></td></tr></table></figure>

<p>以上的信息，你需要根据你自己的情况输入。在最后输入 <code>&quot;yes&quot;</code> 后会生成 <code>package.json</code> 文件。</p>
<p>接下来我们可以使用以下命令在 npm 资源库中注册用户（使用邮箱注册）：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ npm adduser</span><br><span class="line"></span><br><span class="line">Username: mcmohd</span><br><span class="line">Password:</span><br><span class="line">Email: (this IS public) mcmohd@gmail.com</span><br></pre></td></tr></table></figure>

<p>接下来我们就用以下命令来发布模块：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm publish</span><br></pre></td></tr></table></figure>

<p>如果你以上的步骤都操作正确，你就可以跟其他模块一样使用 npm 来安装。</p>
<h1 id="版本号"><a href="#版本号" class="headerlink" title="版本号"></a>版本号</h1><p>使用 NPM 下载和发布代码时都会接触到版本号。NPM 使用语义版本号来管理代码，这里简单介绍一下。</p>
<p>==语义版本号分为 X.Y.Z 三位，分别代表主版本号、次版本号和补丁版本号。== 当代码变更时，版本号按以下原则更新。</p>
<ul>
<li>如果只是修复bug，需要更新Z位。</li>
<li>如果是新增了功能，但是向下兼容，需要更新Y位。</li>
<li>如果有大变动，向下不兼容，需要更新X位。</li>
</ul>
<p>版本号有了这个保证后，在申明第三方包依赖时，除了可依赖于一个固定版本号外，还可依赖于某个范围的版本号。例如 <code>&quot;argv&quot;: &quot;0.0.x&quot;</code> 表示依赖于 <code>0.0.x</code> 系列的最新版 <code>argv</code>。</p>
<h1 id="NPM-常用命令"><a href="#NPM-常用命令" class="headerlink" title="NPM 常用命令"></a>NPM 常用命令</h1><p>除了本章介绍的部分外，NPM还提供了很多功能，<code>package.json</code> 里也有很多其它有用的字段。</p>
<p>除了可以在 <code>npmjs.org/doc/</code>  查看官方文档外，这里再介绍一些NPM常用命令。</p>
<ul>
<li>NPM 提供了很多命令，例如 <code>install</code> 和 <code>publish</code> ，使用 <code>npm help</code> 可查看所有命令。</li>
<li>使用 <code>npm help &lt;command&gt;</code> 可查看某条命令的详细帮助，例如 <code>npm help install</code>。</li>
<li>在 <code>package.json</code> 所在目录下使用 <code>npm install . -g</code>  可先在本地安装当前命令行程序，可用于发布前的本地测试。</li>
<li>使用 <code>npm update &lt;package&gt;</code> 可以把当前目录下 <code>node_modules</code> 子目录里边的对应模块更新至最新版本。</li>
<li>使用 <code>npm update &lt;package&gt; -g</code> 可以把全局安装的对应命令行程序更新至最新版。</li>
<li>使用 <code>npm cache clear</code> 可以清空 NPM 本地缓存，用于对付使用相同版本号发布新版本代码的人。</li>
<li>使用 <code>npm unpublish &lt;package&gt;@&lt;version&gt;</code> 可以撤销发布自己发布过的某个版本代码。</li>
</ul>
<h1 id="使用淘宝-NPM-镜像"><a href="#使用淘宝-NPM-镜像" class="headerlink" title="使用淘宝 NPM 镜像"></a>使用淘宝 NPM 镜像</h1><p>淘宝 NPM 镜像是一个完整 npmjs.org 镜像，你可以用此代替官方版本(只读)，同步频率目前为 10分钟 一次以保证尽量与官方服务同步。</p>
<p>你可以使用淘宝定制的 <code>cnpm</code> (gzip 压缩支持) 命令行工具代替默认的 <code>npm</code> :</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm install -g cnpm --registry&#x3D;https:&#x2F;&#x2F;registry.npm.taobao.org</span><br></pre></td></tr></table></figure>

<p>这样就可以使用 cnpm 命令来安装模块了：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cnpm install [name]</span><br></pre></td></tr></table></figure>

<p>更多信息可以查阅：<a href="http://npm.taobao.org/" target="_blank" rel="noopener">http://npm.taobao.org/</a> 。</p>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/03/08/react%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E5%8F%8C%E5%90%91%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A/" rel="prev" title="react实现简单双向数据绑定">
      <i class="fa fa-chevron-left"></i> react实现简单双向数据绑定
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/12/15/postcss-loader-%E8%87%AA%E5%8A%A8%E6%B7%BB%E5%8A%A0%E6%A0%B7%E5%BC%8F%E5%89%8D%E7%BC%80/" rel="next" title="postcss-loader 自动添加样式前缀">
      postcss-loader 自动添加样式前缀 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#介绍"><span class="nav-number">1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用-npm-命令安装模块"><span class="nav-number">2.</span> <span class="nav-text">使用 npm 命令安装模块</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#全局安装与本地安装"><span class="nav-number">3.</span> <span class="nav-text">全局安装与本地安装</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#本地安装"><span class="nav-number">3.1.</span> <span class="nav-text">本地安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#全局安装"><span class="nav-number">3.2.</span> <span class="nav-text">全局安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查看安装信息"><span class="nav-number">3.3.</span> <span class="nav-text">查看安装信息</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#几种安装方式的区别-g、–save、–save-dev"><span class="nav-number">4.</span> <span class="nav-text">几种安装方式的区别(-g、–save、–save-dev)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#npm-install-moduleName-命令"><span class="nav-number">4.1.</span> <span class="nav-text">npm install moduleName 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#npm-install-g-moduleName-命令"><span class="nav-number">4.2.</span> <span class="nav-text">npm install -g moduleName 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#npm-install-save-moduleName-命令"><span class="nav-number">4.3.</span> <span class="nav-text">npm install -save moduleName 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#npm-install-save-dev-moduleName-命令"><span class="nav-number">4.4.</span> <span class="nav-text">npm install -save-dev moduleName 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">4.5.</span> <span class="nav-text">总结</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用-package-json"><span class="nav-number">5.</span> <span class="nav-text">使用 package.json</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Package-json-属性说明"><span class="nav-number">6.</span> <span class="nav-text">Package.json 属性说明</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#卸载模块"><span class="nav-number">7.</span> <span class="nav-text">卸载模块</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#更新模块"><span class="nav-number">8.</span> <span class="nav-text">更新模块</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#搜索模块"><span class="nav-number">9.</span> <span class="nav-text">搜索模块</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#创建模块"><span class="nav-number">10.</span> <span class="nav-text">创建模块</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#版本号"><span class="nav-number">11.</span> <span class="nav-text">版本号</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#NPM-常用命令"><span class="nav-number">12.</span> <span class="nav-text">NPM 常用命令</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用淘宝-NPM-镜像"><span class="nav-number">13.</span> <span class="nav-text">使用淘宝 NPM 镜像</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Albert Guo"
      src="https://inews.gtimg.com/newsapp_ls/0/12122532695/0">
  <p class="site-author-name" itemprop="name">Albert Guo</p>
  <div class="site-description" itemprop="description">郭鹏松的个人博客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">19</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="mailto:albert-guo@outlook.com" title="E-Mail → mailto:albert-guo@outlook.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Albert Guo</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
